package com.lee.forum.controller;

import com.lee.forum.common.AppResult;
import com.lee.forum.common.ResultCode;
import com.lee.forum.model.Board;
import com.lee.forum.services.IBoardService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-09-30
 * Time: 13:22
 */
@Api(tags = "板块接口")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    // 从配置文件中读取，如果没有值，默认值为9
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    // 注入service
    @Resource
    private IBoardService boardService;


    /**
     * 查询首页板块列表
     * @return
     */
    @ApiOperation("查询首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        // 1.打印日志
        log.info("首页板块个数为："+indexBoardNum);
        // 2.调用service查询结果
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        // 3.判断结果
        if (boards == null) {
            boards = new LinkedList<>();
        }
        // 4.返回结果
        return AppResult.success(boards);
    }

    /**
     * 指定板块Id获取板块信息
     * @param id 板块id
     * @return
     */
    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@ApiParam("板块Id") @RequestParam("id") @NotNull Long id) {
        // 1.调用Service查询板块信息
        Board board = boardService.selectById(id);
        // 2.校验查询结果
        if (board == null || board.getDeleteState() == 1 || board.getState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_STATUS.toString());
            // 返回错误状态信息
            return AppResult.failed(ResultCode.FAILED_BOARD_STATUS);
        }
        // 3.返回结果
        return AppResult.success(board);
    }
}
